﻿using System;
using System.Linq;
using System.Linq.Dynamic;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using VCC.CMS.Properties;
using VCC.CMS.ViewModels;
using VCC.Lib.Common;
using VCC.Lib.Helpers;
using VCC.Lib.Paging;
using VCC.Model;
namespace VCC.CMS.Controllers
{
	[RoleHelper(Roles = RoleSystem.Menu)]
    public class MenuController : Controller
    {
        //
        // GET: /Menu/
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        public ActionResult Index()
        {
            MenuViewIndexModel m = new MenuViewIndexModel();
            try
            {
                    string order = UserState.getUserStateFromRequest("menu.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("menu.orderDir", "orderDir", "desc").ToString();

                    string keyword = UserState.getUserStateFromRequest("menu.Keyword", "Keyword", "").ToString();
                    var typeid = Utils.ParseInt(UserState.getUserStateFromRequest("menu.Type", "ddlMenuType", 0));

                    var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("menu.PageNumber", "page", 1));
                    var pageIndex = pageNumber - 1;
                    var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("menu.PageSize", "pageSize", 20));

                    var menus = eDataContext.Menus
                        .Where(a => typeid > 0 ? a.TypeID == typeid : true)                        
                        .Where(a => string.IsNullOrEmpty(keyword) ? true :
                            a.Name.ToLower().Contains(keyword.ToLower().Trim()))
                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                        .ToPagedList(pageIndex, pageSize);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    m.ParentMenus = menus.Where(e => e.ParentID == null).OrderBy(e => e.Priority).ToList();
                    m.Menus = menus;
                    m.MenuTypes = eDataContext.MenuTypes.ToList();
                    m.FilterName = keyword;
					m.CurrentMenuType = typeid;
                    return View(m);
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = "0#" + exp.Message;
                return View(m);
            }
        }

        //
        // GET: /Menu/Details/5
        public ActionResult Details(int id)
        {
            try
            {
                    var item = eDataContext.Menus.SingleOrDefault(e => e.ID == id);
                    return View(item);
            }
            catch (Exception exp)
            {
                return View();
            }
        }

        //
        // GET: /Menu/Create
        public ActionResult Create()
        {
            try
            {
                MenuViewEditModel model = new MenuViewEditModel();
                model.MenuTypes = eDataContext.MenuTypes.ToList();
                model.Menus = eDataContext.Menus.ToList();
                model.Categories = CategoryManager.BuiltCombobox(0);
                return View(model);
            }
            catch (Exception exp)
            {
                return View();
            }
        } 

        //
        // POST: /Menu/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(MenuViewEditModel m)
        {
            try
            {
                    UpdateModel(m);
                    m.Menu.ModifyBy = UserManager.GetUserId();
                    m.Menu.ModifyDate = DateTime.Now;
                    m.Menu.AddBy = UserManager.GetUserId();
                    m.Menu.AddDate = DateTime.Now;

                    eDataContext.Menus.InsertOnSubmit(m.Menu);
                    eDataContext.SubmitChanges();
                   
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = "0#" + exp.Message;
                return View();
            }
        }
        
        
       //  GET: /Menu/Edit/5
        public ActionResult Edit(int id)
        {
            try
            {
                    MenuViewEditModel model = new MenuViewEditModel();
                    model.Menu = eDataContext.Menus.Where(e => e.ID == id).SingleOrDefault();
                    model.MenuTypes = eDataContext.MenuTypes.ToList();
                    model.Menus = eDataContext.Menus.ToList();
                    model.Categories = CategoryManager.BuiltCombobox(0);
                    return View(model);
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = "0#" + exp.Message;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Menu/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, MenuViewEditModel m)
        {
            try
            {
                m.Menu = eDataContext.Menus.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(m);
                m.Menu.ModifyBy = UserManager.GetUserId();
                m.Menu.ModifyDate = DateTime.Now;
                eDataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = "0#" + exp.Message;
                return View(m);
            }
        }

        //
        // GET: /Menu/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                    var item = eDataContext.Menus.SingleOrDefault(e => e.ID == id);
                    return View(item);
            }
            catch (Exception exp)
            {
               
                return View();
            }
        }

        //
        // POST: /Menu/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                    var item = eDataContext.Menus.SingleOrDefault(e => e.ID == id);
                    if (item != null)
                    {
                        eDataContext.Menus.DeleteAllOnSubmit(from m in eDataContext.Menus where m.ParentID==id select m);
                        eDataContext.Menus.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = "0#" + exp.Message;
                return View();
            }
        }
    }
}
